﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class WashFactorRemarkRepo
    {
        public const string m_msg_head = "WashFactorRemark";

        public IEnumerable<WashFactorRemarkModels> GetWashFactorRemark()
        {
            List<WashFactorRemarkModels> WashFactorRemarks = new List<WashFactorRemarkModels>();
            string SQLCommand = @"select * from TBWashFactorRemark WHERE Status <> 'Delete'";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        WashFactorRemarkModels WashFactorRemark = new WashFactorRemarkModels();
                        WashFactorRemark.Id = (int)reader["Id"];
                        WashFactorRemark.TitleWashFactorsType = (string)reader["TitleWashFactorsType"];
                        WashFactorRemark.WashFactorRemarkTh = (string)reader["WashFactorRemarkTh"];
                        WashFactorRemark.WashFactorRemarkEng = (string)reader["WashFactorRemarkEng"];
                        WashFactorRemark.Status = (string)reader["Status"];
                        WashFactorRemark.CreateBy = (string)reader["CreateBy"];
                        WashFactorRemark.CreateOn = (DateTime)reader["CreateOn"];
                        WashFactorRemark.UpdateBy = (string)reader["UpdateBy"];
                        WashFactorRemark.UpdateOn = (DateTime)reader["UpdateOn"];

                        WashFactorRemarks.Add(WashFactorRemark);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return WashFactorRemarks.ToList();
        }

        public WashFactorRemarkModels GetWashFactorRemarkById(int id)
        {
            WashFactorRemarkModels WashFactorRemark = new WashFactorRemarkModels();
            string SQLCommand = @"select * from TBWashFactorRemark where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@Id", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        WashFactorRemark.Id = (int)reader["Id"];
                        WashFactorRemark.TitleWashFactorsType = (string)reader["TitleWashFactorsType"];
                        WashFactorRemark.WashFactorRemarkTh = (string)reader["WashFactorRemarkTh"];
                        WashFactorRemark.WashFactorRemarkEng = (string)reader["WashFactorRemarkEng"];
                        WashFactorRemark.Status = (string)reader["Status"];
                        WashFactorRemark.CreateBy = (string)reader["CreateBy"];
                        WashFactorRemark.CreateOn = (DateTime)reader["CreateOn"];
                        WashFactorRemark.UpdateBy = (string)reader["UpdateBy"];
                        WashFactorRemark.UpdateOn = (DateTime)reader["UpdateOn"];
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return WashFactorRemark;
        }

        public bool Create(WashFactorRemarkModels WashFactorRemark)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBWashFactorRemark 
                                    values (@TitleWashFactorsType,@WashFactorRemarkTh,@WashFactorRemarkEng,@Status,@UserId,
                                    GETDATE(),@UserId,GETDATE())";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleWashFactorsType", WashFactorRemark.TitleWashFactorsType);
                    db.AddParameter("@WashFactorRemarkTh", WashFactorRemark.WashFactorRemarkTh);
                    db.AddParameter("@WashFactorRemarkEng", WashFactorRemark.WashFactorRemarkEng);
                    db.AddParameter("@Status", "Active");
                    db.AddParameter("@UserID",WashFactorRemark.CreateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(WashFactorRemarkModels WashFactorRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBWashFactorRemark 
                                    Set TitleWashFactorsType= @TitleWashFactorsType,
                                        WashFactorRemarkTh=@WashFactorRemarkTh,
                                        WashFactorRemarkEng=@WashFactorRemarkEng,
                                        UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", WashFactorRemark.Id);
                    db.AddParameter("@TitleWashFactorsType", WashFactorRemark.TitleWashFactorsType);
                    db.AddParameter("@WashFactorRemarkTh", WashFactorRemark.WashFactorRemarkTh);
                    db.AddParameter("@WashFactorRemarkEng", WashFactorRemark.WashFactorRemarkEng);
                    db.AddParameter("@UserID",WashFactorRemark.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(WashFactorRemarkModels WashFactorRemark)
        {
            bool result = true;
            string SQLCommand = @"Update TBWashFactorRemark 
                                    Set Status=@Status,UpdateBy=@UserId,UpdateOn=getdate()
                                    where Id=@Id";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@Id", WashFactorRemark.Id);
                    db.AddParameter("@Status", "Delete");
                    db.AddParameter("@UserID", WashFactorRemark.UpdateBy);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}